﻿package com.wabbei.fenxiao.action;

import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.taobao.api.internal.stream.message.TopCometMessageListener;
import com.webbei.myCustom.service.impl.myCustomServiceImpl;
import com.webbei.pojo.Orders;
import com.webbei.shoppingCar.service.impl.shoppingCarServiceImpl;

public class TopCometMessageListenerImpl implements TopCometMessageListener{
	private String appPath=data.appPath;
	public void onReceiveMsg(String msg) {
		System.out.println("-------------onReceiveMsg-------------");
		System.out.println(msg);
		ApplicationContext ac = new ClassPathXmlApplicationContext(appPath);
		shoppingCarServiceImpl scs = (shoppingCarServiceImpl) ac.getBean("shoppingCarService");
		myCustomServiceImpl mcs = (myCustomServiceImpl) ac.getBean("customService");
		MyTaobaoUtil mt=new MyTaobaoUtil();
		JSONObject jb = JSONObject.fromObject(msg);
		JSONObject ja = null;
//---------------------------------------------------------以下为交易信息------------------------------------------------------------------
		if(jb.containsKey("notify_trade")){
			ja=jb.getJSONObject("notify_trade");
//-------------------------------------------------------网贝旗舰店的交易信息----------------------------------------------------------------
			if(ja.getString("seller_nick").equals(data.name1)){
				if(ja.getString("status").equals("TradeBuyerPay")){
					System.out.println(data.name1+"买家付款");
					List<Orders> trade=mt.getOrderBytid(Long.parseLong(ja.getString("tid")),data.sessionKey1);
					for(Orders o:trade){
						scs.saveOrder2(o);
						System.out.println("订单保存成功");
					}
				}else if(ja.getString("status").equals("TradeSellerShip")){
					System.out.println(data.name1+"卖家发货");
					scs.updateOrderStateOfTradeSellerShip(Long.parseLong(ja.getString("tid")));
				}else if(ja.getString("status").equals("TradeMemoModified")){
					System.out.println(data.name1+"交易备注修改");
					mt.changMemo(Long.parseLong(ja.getString("tid")),data.sessionKey1);
				}else if(ja.getString("status").equals("TradeSuccess")){
					System.out.println(data.name1+"交易成功");
					scs.changOrderStateToThree(Long.parseLong(ja.getString("tid")));
				}else{
					System.out.println(data.name1+"未知交易信息："+ja.getString("status"));
				}
//-------------------------------------------------------代洋洋816的交易信息----------------------------------------------------------------
			}else if(ja.getString("seller_nick").equals(data.name2)){
				if(ja.getString("status").equals("TradeBuyerPay")){
					System.out.println(data.name2+"买家付款");
					List<Orders> trade=mt.getOrderBytid(Long.parseLong(ja.getString("tid")),data.sessionKey2);
					for(Orders o:trade){
						scs.saveOrder2(o);
						System.out.println("的这笔订单保存成功");
					}
				}else if(ja.getString("status").equals("TradeSellerShip")){
					System.out.println(data.name2+"卖家发货");
					scs.updateOrderStateOfTradeSellerShip(Long.parseLong(ja.getString("tid")));
				}else if(ja.getString("status").equals("TradeMemoModified")){
					System.out.println(data.name2+"交易备注修改");
					mt.changMemo(Long.parseLong(ja.getString("tid")),data.sessionKey2);
				}else if(ja.getString("status").equals("TradeSuccess")){
					System.out.println(data.name2+"交易成功");
					scs.changOrderStateToThree(Long.parseLong(ja.getString("tid")));
				}else{
					System.out.println(data.name2+"未知交易信息："+ja.getString("status"));
				}
			}else{
				System.out.println("交易消息未识别店铺信息");
			}
//---------------------------------------------------------向上为交易信息,以下为退款信息------------------------------------------------------------------
		}else if(jb.containsKey("notify_refund")){
			ja=jb.getJSONObject("notify_refund");
//---------------------------------------------------------网贝旗舰店的退款信息----------------------------------------------------------------
			if(ja.getString("seller_nick").equals(data.name1)){
				if(ja.getString("status").equals("RefundCreated")){
					System.out.println("退款创建");
					scs.changOrderStateToZero(Long.parseLong(ja.getString("tid")));
				}else if(ja.getString("status").equals("RefundSuccess")){
					System.out.println("退款成功");
					String refund_id=ja.getString("refund_id");
					scs.changOrderStateToFour(Long.parseLong(ja.getString("tid")));
					scs.saveRefundOrder(refund_id,MyTaobaoUtil.getSessionKey(ja.getString("seller_nick")));
				}else{
					System.out.println("未知退款信息："+ja.getString("status"));
				}
//---------------------------------------------------------代洋洋816的退款信息----------------------------------------------------------------
			}else if(ja.getString("seller_nick").equals(data.name2)){
				if(ja.getString("status").equals("RefundCreated")){
					System.out.println("退款创建");
					scs.changOrderStateToZero(Long.parseLong(ja.getString("tid")));
				}else if(ja.getString("status").equals("RefundSuccess")){
					System.out.println("退款成功");
					String refund_id=ja.getString("refund_id");
					scs.changOrderStateToFour(Long.parseLong(ja.getString("tid")));
					scs.saveRefundOrder(refund_id,MyTaobaoUtil.getSessionKey(ja.getString("seller_nick")));
					//mcs.deleOrder(Long.parseLong(ja.getString("oid")));
				}else{
					System.out.println("未知退款信息："+ja.getString("status"));
				}
			}else{
				System.out.println("退款消息未识别店铺信息");
			}
		}else{
			System.out.println("未知通知："+msg);
		}
		System.out.println("-------------onReceiveMsg-------------");
	}
//------------------------------------------------------------------------------------------------------------------------------------------

	public void onClientKickOff() {
		System.out.println("客户端踢出");
	}
	public void onConnectMsg(String arg0) {
		System.out.println("连接信息："+arg0);
	}
	public void onConnectReachMaxTime() {
		System.out.println("连接达到最大时间");
	}
	public void onDiscardMsg(String arg0) {
		System.out.println("信息丢失："+arg0);
	}

	public void onException(Exception arg0) {
		System.out.println("异常："+arg0);
	}

	public void onHeartBeat() {
		//System.out.println("[Heart-Beat]");
	}

	public void onOtherMsg(String arg0) {
		System.out.println("另外信息："+arg0);
	}

	public void onServerKickOff() {
		System.out.println("已经达到这个连接下发消息的瓶颈（服务器踢出-105错误）");
	}

	public void onServerRehash() {
		System.out.println("服务更改");
	}

	public void onServerUpgrade(String arg0) {
		System.out.println("服务升级："+arg0);
	}


}
